<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of SystemAction
 *
 * @author Administrator
 */
class SystemAction extends BaseAction {
    //put your code here
    public function version(){
        $m = M("Version");
        import("ORG.Util.Page");
        $count = $m->count();
        $p = new Page($count,C("PAGE_SIZE"));
        $data = $m->join(" department as t on depart_id =  t.id")->order("release_date desc")->limit($p->firstRow.','.$p->listRows)->select();
        $page = $p->show();
        $this->assign("version_list",$data);
        $this->assign("page",$page);
        
        $departs = A('Department')->get_hosts_str(session('currdepartid'));
		if ($departs != null){
			$this->assign('hosts',M('Host')->where("DepartId in ($departs)")->getField('Id,HostName'));
		}
		$this->assign('departs',A('Department')->get_departments_for_select(session('currdepartid')));
                
                
        $this->display();
        
    }
    
    public function version_add(){
        $this->get_authority('System','version','Add');
        import('ORG.Net.UploadFile');
        $upload = new UploadFile();
        //$upload->allowExts = array('*');
        $upload->savePath = C("VERSION_PATH");
        $upload->saveRule = '';
        if (!$upload->upload()){
            $this->error($upload->getErrorMsg());
        }
        
        $version_file = $upload->getUploadFileInfo();
        
        $v = M('Version');
        if ($v->create()){
            
            $v->release_date = date('Y-m-d H:i:s');
            $v->version_file = C("VERSION_PATH").$version_file[0]["savename"];

            $result = $v->add();

            if($result){
                    redirect(__URL__.'/version');
            }
            else{
                    $this->error('写入错误!');
            }
        }
        else{
                $this->error($v->getError());
        }
    }
    
    public function version_remove(){
        $this->get_authority('System','version','Delete');
        $id = $_POST['ids'];
        if(!$id){
                $this->error('未选择要删除的版本!');
        }
        $ids = implode("','",$id);
        
        $result = M('Version')->where("id in ('".$ids."')")->delete();

        if($result){
                redirect(__URL__.'/version');
        }
        else{
                $this->error('删除出错!');
        }
    }
    /*
     * 基本资料-参数设置
     */
    public function  parametersettings(){
    	
        //print_r(C());
        $product = C('product');
        $cent_db = C('DB_CSG');
        $this->assign('product',$product);
        $this->assign('cent_db',$cent_db);
        
        $this->display();
    }
    
    public function parameters_update(){
    	
    	
        $config['WEB_NAME'] = $_POST['web_name'];
        $config['WEB_VERSION'] = $_POST['web_version'];
        $config['PAGE_SIZE'] = $_POST['page_size'];
        $config['CHART_LIMIT'] = $_POST['chart_limit'];
        $config['DEFAULT_THEME'] = $_POST['default_theme'];
        $config['LAYOUT_ON'] = $_POST['layout_on']=='on'?1:0;
        $config['LAYOUT_NAME'] = $_POST['layout_name'];
        $config['UPLOAD_PATH'] = $_POST['upload_path'];
        $config['VERSION_PATH'] = $_POST['version_path'];
        
        $config['DB_TYPE'] = $_POST['db_type'];
        $config['DB_HOST'] = $_POST['db_host'];
        $config['DB_NAME'] = $_POST['db_name'];
        $config['DB_USER'] = $_POST['db_user'];
        $config['DB_PWD'] = $_POST['db_pwd'];
        $config['DB_PORT'] = $_POST['db_port'];
        $config['DB_PREFIX'] = $_POST['db_prefix'];
        
        $config['CtrlCenter'] = $_POST['ctrlcenter'];
        
        $product['productid'] = $_POST['productid'];
        $product['productname'] = $_POST['productname'];
        $config['product'] = $product;
        
        $cent_db['DB_TYPE'] = $_POST['cent_db_type'];
        $cent_db['DB_NAME'] = $_POST['cent_db_name'];
        $cent_db['DB_USER'] = $_POST['cent_db_user'];
        $cent_db['DB_PWD'] = $_POST['cent_db_pwd'];
        $cent_db['DB_PREFIX'] = $_POST['cent_db_prefix'];
        $cent_db['DB_PORT'] = $_POST['cent_db_port'];
        $cent_db['DB_HOST'] = $_POST['cent_db_host'];
        
        $config['DB_CSG'] = $cent_db;
                
        $content = $this->array_to_string($config);
        //print $_SERVER["PHP_SELF"];exit;
        //print $content;

        copy('/var/www/necache_web/Conf/config.php', '/var/www/necache_web/Conf/config.php.'.date("Y_m_d_H_i_s"));
        $content = "<?php\r\nreturn ".$content.';';
        file_put_contents('/var/www/necache_web/Conf/config.php', $content);
        
       // C($config,'config');
                
        $this->redirect("parametersettings");
    }
    
    private function array_to_string($arr,$level=0){
        if (!is_array($arr)){
            return;
        }
        $output = "array( \r\n";
        foreach($arr as $key=>$val){

            if(is_array($val)){
                $tmp = $this->array_to_string($val,$level+1);
                $output .= str_repeat(' ',($level+1)*4)."'$key'=>$tmp,\r\n";
            }
            else{
                $output .= str_repeat(' ',($level+1)*4)."'$key'=>'$val',\r\n";
            }
            
        }
        $output .= str_repeat(' ',$level*4).')';
        //$output= substr($output,0,strlen($output)-2).str_repeat(' ',$level*4).')';
        return $output;
    }

    public function modules(){$this->error("thsi is a test");
        $m = new Model();
        $data = $m->query("select t.*,v.MenuName as p_name,ifnull(t.Pid,t.Id) as p_id from menu t left join menu v on t.Pid=v.Id  order by  if(ifnull(t.Pid,0)=0,t.Id,t.Pid),t.Pid,t.Id");
        
        $parent_id = M("menu")->where("ifnull(Pid,0) = 0")->getField("Id,MenuName");
        
        $this->assign("parent_id",$parent_id);
        
        //print_r($parent_id);exit;
        
        $this->assign("data",$data);
        $this->display();
    }
    
    public function modules_add(){
        $this->get_authority('System','modules','Add');
        $v = M('menu');
        if ($v->create()){
            
            $v->Add = 1;
            $v->Update = 1;
            $v->Delete = 1;
            $v->Report = 1;
            
            if ($_POST['View'] == 'on'){
                $v->View = 1;
            }

            $result = $v->add();

            if($result){
                    redirect(__URL__.'/modules');
            }
            else{
                    $this->error('写入错误!');
            }
        }
        else{
                $this->error($v->getError());
        }
    }
    
    public function modules_remove(){
        $this->get_authority('System','modules','Delete');
        $id = $_POST['ids'];
        if(!$id){
                $this->error('未选择要删除的模块!');
        }
        $ids = implode(",",$id);
        
        $result = M('menu')->where("Id in (".$ids.")")->delete();

        if($result){
                redirect(__URL__.'/modules');
        }
        else{
                $this->error('删除出错!');
        }
    }
    
    public function modules_edit(){
        
        $this->get_authority('System','modules','Update');
        $id = $_GET['module_id'];
         if(!$id){
                $this->error('未选择要修改的模块!');
        }
        
        $m = new Model();
        $data = $m->query("select t.*,v.MenuName as p_name,ifnull(t.Pid,t.Id) as p_id from menu t left join menu v on t.Pid=v.Id  order by  if(ifnull(t.Pid,0)=0,t.Id,t.Pid),t.Pid,t.Id");
        
        $parent_id = M("menu")->where("ifnull(Pid,0)=0")->getField("Id,MenuName");
        
        $this->assign("parent_id",$parent_id);
        
        $result = M('menu')->find($id);
        //print_r($result);exit();
        $this->assign('menuinfo',$result);
        $this->assign("title","模块管理");
        $this->display();
    }
    
    public function modules_update(){
        
        $this->get_authority('System','modules','Update');
        
        $pid =  $_POST['Pid'];
        
        $model = M("menu");
        
        if($model->create()){
            
            $model->Add = 1;
            $model->Update = 1;
            $model->Delete = 1;
            $model->Report = 1;
            
            if($pid == ''){
                $model->Pid = 'NULL';
                print isset($pid);
            }
            //exit;
            if ($_POST['View'] == 'on'){
                $model->View = 1;
            }
            else{
                $model->View = 0;
            }
            $result = $model->save();
            
            if($result) {
                $this->redirect("modules");
            }
            else{
                //$this->error("写入错误");
            }
        }
        else {
                $this->error($m->getError());
        }
    }
    
    /****************************** 用户管理 **********************************/
    
    /*
     * 用户列表
     */
    public function users(){
        
        $m = new Model();
        $data = $m->query('select t.*,u.LevelName from user t
                   left join userlevel u on u.Id = t.UserLevel order by Id');
        
        //$depart_id = M('department')->where('Status=0')->order('Id')->getField("Id,DepartName");
        $levels = M("userlevel")->order('Id')->getField("Id,LevelName");
        $this->assign("data",$data);
        //$this->assign("depart_id",$depart_id);
        $this->assign("levels",$levels);
        $this->display();
    }
    /*
     * 新增用户
     */
    public function users_add(){
        $this->get_authority('System','users','Add');
        $name = $_POST['UserName'];
        $result = M('user')->where("UserName='$name'")->select();
        if ($result){
             $this->error('用户名已经存在!');
             return;
        }
        
        $v = M('user');
        if ($v->create()){
                        
            if ($_POST['Status'] == 'on'){
                $v->Status = 1;
            }
            
            $v->CreateTime = date("Y-m-d H:i:s");

            $result = $v->add();

            if($result){
                    redirect(__URL__.'/users');
            }
            else{
                    $this->error('写入错误!');
            }
        }
        else{
                $this->error($v->getError());
        }
    }
    
    /*
     * 删除用户
     */
    public function users_remove(){
        $this->get_authority('System','users','Delete');
        $id = $_POST['ids'];
        if(!$id){
                $this->error('未选择要删除的用户!');
        }
        $ids = implode("','",$id);
        
        $result = M('user')->where("Id in ('".$ids."')")->delete();

        if($result){
                redirect(__URL__.'/users');
        }
        else{
                $this->error('删除出错!');
        }
    }
    
    /*
     * 检查用户名是否存在
     */
    public function check_user_name($username){

        $result = M('user')->where("UserName='$username'")->select();
        if ($result){
             return false;
        }
        return true;
    }
    
    public function users_edit(){
        $this->get_authority('System','users','Update');
        $id = $_GET['id'];
         if(!$id){
                $this->error('未选择要修改的用户!');
        }

        $m = new Model();
        $data = $m->query("select t.* from user t where Id=$id");
        //print_r($data);exit;
        //$depart_id = M('department')->where('Status=0')->getField("Id,DepartName");
        $levels = M("userlevel")->getField("Id,LevelName");
        $this->assign("data",$data[0]);
        //$this->assign("depart_id",$depart_id);
        $this->assign("levels",$levels);
        $this->assign("title","用户管理");
        $this->display();
    }
    
    public function users_update(){
        $this->get_authority('System','users','Update');
        $model = M("user");
        
        if($model->create()){
                        
            if ($_POST['Status'] == 'on'){
                $model->Status = 1;
            }
            else{
                $model->Status = 0;
            }
            $result = $model->save();
            
            if($result) {
                $this->redirect("users");
            }
            else{
                $this->error($model->getError());
            }
        }
        else {
                $this->error($model->getError());
        }
    }
    
    public function levels(){
        $data = M('userlevel')->order('Seq')->select();
        
        $this->assign('data',$data);
        $this->display();
    }
    
    public function levels_add(){
        
        $this->get_authority('System','levels','Add');
        $v = M('userlevel');
        if ($v->create()){
            $result = $v->add();

            if($result){
                    redirect(__URL__.'/levels');
            }
            else{
                    $this->error('写入错误!');
            }
        }
        else{
                $this->error($v->getError());
        }
    }
    
    public function levels_remove(){
        $this->get_authority('System','levels','Delete');
        $id = $_GET['id'];
        if(!$id){
                $this->error('请选择要删除的内容!');
        }
       
        $result = M('userlevel')->where("Id = $id")->delete();

        if($result){
                redirect(__URL__.'/levels');
        }
        else{
                $this->error('删除出错!');
        }
    }
    
    public function levels_update(){
        
        $this->get_authority('System','levels','Update');
        
        $id = $_POST['id'];
        $levelname = $_POST['levelname'];
        $leveldesc = $_POST['leveldesc'];
        $seq = $_POST['seq'];
        
        $Dao = M('userlevel');
        /*
        print_r($id);print '<br/>';
        print_r($levelname);print '<br/>';
        print_r($leveldesc);print '<br/>';
        print_r($seq);print '<br/>';
        */
        foreach ($id as $key => $value) {

            $data['LevelName'] = $levelname[$key];
            $data['LevleDesc'] = $leveldesc[$key];
            $data['Seq'] = $seq[$key];
            
            $condition['Id'] = $value;
             $Dao->where($condition)->data($data)->save();
            
            
        }
        
        $result = $Dao->commit();

        if(!$result){
            $this->error($Dao->getError());
        }
        
        $this->redirect('levels');
        
    }
    
    /*******************************权限管理**********************************/
    public function authority(){
        
        $levelid = $_GET['UserLevel'];

        $levels = M("userlevel")->order('Seq')->getField("Id,LevelName");
        $this->assign("levels",$levels);

        $key = (array_keys($levels));
        
        if(!$levelid && count($key)>0){
            $levelid = $key[0];
        }
        
        $dao = new Model();
        $data = $dao->query("select t.*,v.MenuName,v.MenuDesc,u.MenuName as p_name from userauthority t 
            left join menu v on t.MenuId=v.Id
            left join menu u on v.Pid = u.Id and ifnull(u.Pid,0)=0
            where t.Type=1 and t.LevelId=$levelid order by  if(ifnull(v.Pid,0)=0,v.Id,v.Pid),v.Pid,v.Id");
        $this->assign('data',$data);
        
        $this->assign('current_level',$levelid);
        $this->display();
    }
    
    public function authority_update(){
        
        $cur_level = $_POST['curr_level'];
        $type = $_POST['Type'];

        $menuids = $_POST['menuid'];
        $views = explode(",",$_POST['views']);
        $adds = explode(",",$_POST['adds']);
        $updates = explode(",",$_POST['updates']);
        $deletes = explode(",",$_POST['deletes']);
        $specials = $_POST['Special'];
   
        $Dao = M('userauthority');

        foreach ($menuids as $key => $value) {

            $data['View'] = $views[$key];
            $data['Add'] = $adds[$key];
            $data['Update'] = $updates[$key];
            $data['Delete'] = $deletes[$key];
            $data['Special'] = $specials[$key];
            
            $condition['Type'] = $type;
            $condition['LevelId'] = $cur_level;
            $condition['MenuId'] = $value;
            
             $Dao->where($condition)->data($data)->save();

        }
        if($type == 1){
            $this->redirect("authority?UserLevel=$cur_level");
        }
        else{
            $this->redirect("userauthority?UserLevel=$cur_level");
        }
        
    }
    
    public function userauthority(){
        
        $levelid = $_GET['UserLevel'];

        $levels = M("user")->getField("Id,UserName");
        $this->assign("levels",$levels);
        
        $key = (array_keys($levels));
        
        if(!$levelid && count($key)>0){
            $levelid = $key[0];
        }
        
        $dao = new Model();
        $data = $dao->query("select t.*,v.MenuName,v.MenuDesc,u.MenuName as p_name from userauthority t 
            left join menu v on t.MenuId=v.Id
            left join menu u on v.Pid = u.Id and ifnull(u.Pid,0)=0
            where t.Type=2 and t.LevelId=$levelid order by  if(ifnull(v.Pid,0)=0,v.Id,v.Pid),v.Pid,v.Id");
        $this->assign('data',$data);
        
        $this->assign('current_level',$levelid);
        $this->display();
    }
    
    
}

?>
